package com.linghu.test;

import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.app.ApplicationUsage;
import com.alibaba.dashscope.app.RagApplicationParam;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;

/**
 * @author linghu
 * @date 2024/4/30 11:03
 */
public class MainTest02 {
    public static void ragCall()
            throws ApiException, NoApiKeyException, InputRequiredException {
        Constants.apiKey="sk-W9yPJw0c7Q";
        RagApplicationParam param = RagApplicationParam.builder()
                .appId("899c8e534eb64a40ab9aac9ab20f7414")
                .prompt("您可以通过TDDamHttpUtil.resultUtil方法处理不同请求结果的数据")
                .build();
        Application application = new Application();
        ApplicationResult result = application.call(param);

        System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
                result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());

        if (result.getUsage() != null && result.getUsage().getModels() != null) {
            for (ApplicationUsage.ModelUsage usage : result.getUsage().getModels()) {
                System.out.printf("modelId: %s, inputTokens: %d, outputTokens: %d\n",
                        usage.getModelId(), usage.getInputTokens(), usage.getOutputTokens());
            }
        }
    }

    public static void main(String[] args) {
        try {
            ragCall();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.printf("Exception: %s", e.getMessage());
        }
        System.exit(0);
    }
}
